<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import echarts from 'echarts'
import resize from './mixins/resize'

require('echarts/theme/macarons') // echarts theme

export default {
  name: 'RadarChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      if (this.chartData != null) {
        this.setOptions(this.chartData)
      }
    },
    setOptions({max, name, content} = {}) {
      this.chart.setOption({
        title: {
          text: '能耗占比',
          subtext: '按部门',
          top: 10,
          left: 10
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          type: 'scroll',
          bottom: 10,
          data: name
        },
        radar: {
          indicator: [
            {text: '照明系统', max: max[0]},
            {text: '送排风系统', max: max[1]},
            {text: '空调系统', max: max[2]}
            // ,
            // { text: '控制器系统', max: max[3] },
            // { text: '空调系统', max: max[4] }
          ],
          name: {
            fontSize: 9
          }
        },
        series: (function () {
          var series = []
          for (var i = 0; i < content.length; i++) {
            series.push({
              name: 'sss',
              type: 'radar',
              symbol: 'none',
              lineStyle: {
                width: 1
              },
              emphasis: {
                areaStyle: {
                  color: 'rgba(0,250,0,0.3)'
                }
              },
              data: [{
                value: content[i].value,
                name: content[i].name
              }]
            })
          }
          return series
        })()
      })
    }
  }
}
</script>

<style scoped>

</style>
